<?php
/*
 * 接受微信支付通知
 * 继承的类含有基本接受通知能力
 */
require_once '../../PUBLIC.php';
require '../../core/class/PayBase.php';

class TongZhi extends PayBase
{

    /*构造函数*/
    public function __construct()
    {
        parent::__construct();
        $this->init();
        $this->display();
    }

    //保存微信通知流数据的变量
    protected $streamData = '';

    //初始化
    protected function init()
    {
        $this->logFileManger();
        $this->streamData = $this->readStream();
    }

    //主要工作区域
    public function display()
    {

        if (!empty($this->streamData)) {
            $this->log(date('Y-m-d H:i:s'));
            $this->log($this->streamData);

            //获取数组数据
            $data = $this->xmlToArray($this->streamData);
            ksort($data);
            $buff = '';
            foreach ($data as $k => $v) {
                if ($k != 'sign') {
                    $buff .= $k . '=' . $v . '&';
                } else {
                    $this->log($k . ' ' . $v);
                }
            }

            $stringSignTemp = $buff . 'key=' . KEY;

            $this->log('qian ming temp string');
            $this->log($stringSignTemp);

            $sign = strtoupper(md5($stringSignTemp));
            if ($sign == $data['sign']) {
                $this->log('qian ming ok');

                //更新支付成功状态
                $this->trycatch($data);
                //
                $this->log('now send success to wei xin ! ');
                //发送成功通知
                $this->returnSuccess();
            } else {
                $this->log($sign . ' != ' . $data['sign']);
                $this->log('sign FAIL !');
            }
            //执行结束
            $this->logend();
        }
    }

    //捕捉异常并且记录
    protected function trycatch($data)
    {
        try {
            $this->update($data);
        } catch (Exception $e) {
            $this->log('warn ');
            $this->log($e->getMessage());
        }
    }

    //支付成功后做的操作
    protected function update($data)
    {
        if (isset($data['return_code']) && $data['return_code'] == 'SUCCESS') {
            $this->log('return_code SUCCESS');

            if (isset($data['result_code']) && $data['result_code'] == 'SUCCESS') {
                $this->log('result_code SUCCESS');

                if ($data['appid'] == APPID && $data['mch_id'] == MCHID) {
                    //商户数据正常

                    $this->log('APPID MCHID ok');

                    $openid = $data['openid'];
                    $danhao = $data['out_trade_no'];
                    $this->log('openid = ' . $openid . ', danhao = ' . $danhao);

                    //购买后 状态=0
                    // 查询预约订单记录
                    $query = "select id,fee from " . QZ . "dingDan where openid='{$openid}'
						&& danhao='{$danhao}' && status=0 ";
                    $this->log($query);
                    $YYDD_row = $this->db->query_getrow($query);

                    //$row代表的是订单记录
                    if ($YYDD_row) {
                        $this->log('is mai shui ding dan');

                        //预约订单更新支付状态
                        $query = "update " . QZ . "dingDan set status=1 where
							danhao='{$danhao}' && openid='{$openid}' && status=0 ";
                        $this->log($query);
                        $this->db->exec($query);
                        $this->log('update dingDan dan hao success');

                        /*
                    ///更新绑定的二维码
                    $query = "select * from " . QZ . "user where openid='{$openid}' ";
                    $this->log($query);
                    $userRow = $this->db->query_getrow($query);
                    $uid = $userRow['id'];
                    $ewmid = $userRow['ewmid'];

                    if ($ewmid != 0) {
                    $this->log('user hava erWeiMa');
                    //查询二维码的其他信息
                    $query = "select qian,fee,id from " . QZ . "erWeiMa where id={$ewmid}";
                    $this->log($query);
                    $ewmrow = $this->db->query_getrow($query);
                    if ($ewmrow) {
                    //更新二维码推广所得
                    $query = "update " . QZ . "erWeiMa set qian=qian+{$ewmrow['fee']} where id={$ewmid}";
                    $this->log($query);
                    $this->db->exec($query);
                    $this->log(' erWeiMa updated');

                    //解除用户二维码绑定
                    $query = "update " . QZ . "user set ewmid=0 where id={$uid} and openid='{$openid}' ";
                    $this->log($query);
                    $this->db->exec($query);
                    $this->log(' user updated');

                    //记录佣金发放
                    $query = "insert into " . QZ . "yongJin (
                    ewmid,uid,yyid
                    )values(
                    {$ewmid},{$uid},{$YYDD_row['id']}
                    )";
                    $this->log($query);
                    $this->db->exec($query);
                    $this->log(' yongJin updated');

                    } else {
                    //  '二维码已不存在';
                    $this->log(' erWeiMa not exists');
                    }
                    } else {
                    //  '用户未绑定二维码';
                    $this->log('user not have erWeiMa    ');
                    }
                     */

                    } else {
                        $this->log('is not dingDan ding dan');
                        /*
                    //查询是否是充值
                    $query = "select id,fee from " . QZ . "ChongZhiDingDan where openid='{$openid}'
                    && danhao='{$danhao}' && status=0 ";
                    $this->log($query);
                    $chongZhiRow = $this->db->query_getrow($query);
                    if ($chongZhiRow) {
                    $query = "update " . QZ . "ChongZhiDingDan set status=1 where id=" . $chongZhiRow['id'];
                    $this->log($query);
                    $this->db->exec($query);
                    $this->log('update chong zhi success');
                    $fee = $chongZhiRow['fee'] * 1.2;
                    //更新用户账户余额
                    $query = "update " . QZ . "user set fee = fee + {$fee} where openid = '{$openid}' ";
                    $this->db->exec($query);
                    $this->log($query);
                    } else {
                    $this->log('is not ChongZhiDingDan ding dan');
                    }
                     */
                    }

                } else {
                    $this->log('shang hu zhang hu fail');
                }
            } else {
                $this->log('result_code fail');
            }
        } else {
            $this->log('return_code fail');
        }
    }

    //返回成功消息给微信
    public function returnSuccess()
    {
        $return['return_code'] = 'SUCCESS';
        $return['return_msg'] = 'OK';
        $xml_post =
            '<xml>
				<return_code>' . $return['return_code'] . '</return_code>
				<return_msg>' . $return['return_msg'] . '</return_msg>
			</xml>';
        echo $xml_post;
    }

    //打印日志
    public function logend()
    {
        $this->log('timestamp:' . time());
        $this->log('the end line , bye !');
    }

}
new TongZhi;
